package com.cinsea.hrm.ext;


import com.cinsea.common.Env;
import com.cinsea.common.ExtAction;
import com.cinsea.common.JdbcDao;
import com.cinsea.interfaces.form.Formdata;
import com.cinsea.interfaces.form.FormdataServiceImpl;
import com.cinsea.interfaces.model.Cell;
import com.cinsea.interfaces.model.Dataset;
import com.cinsea.util.DateUtil;
import com.cinsea.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KaoqinAction extends ExtAction {
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String action = StringUtil.null2String(request.getParameter("action"));
        if (action.equals("signin")) {
            JdbcDao jdbcDao=Env.getBean("jdbcDao");
            String currentuser=Env.getRemoteUser().getId();
            String currentDate = DateUtil.getCurrentDate();
            String currentTime = DateUtil.getCurrentTime();
            List tmp=jdbcDao.queryForList("select id from ut_kqjl where humanid=? and carddate=?",new Object[]{currentuser,currentDate});
            if(tmp.size()>0){
                response.getWriter().println("不能重复签到!");
                return;
            }
            Map content = new HashMap();

            content.put("ut_kqjl_humanid", currentuser);
            content.put("ut_kqjl_carddate", currentDate);
            content.put("ut_kqjl_cardtime", currentTime);
            content.put("ut_kqjl_cardip", request.getRemoteAddr());
            makeKqjl(content);
            response.getWriter().println("成功!（签到时间："+currentTime+"）");
        }
        if (action.equals("signout")) {
            Map content = new HashMap();
            String currentDate = DateUtil.getCurrentDate();
            String currentTime = DateUtil.getCurrentTime();
            content.put("ut_kqjl_humanid", Env.getRemoteUser().getId());
            content.put("ut_kqjl_carddate", currentDate);
            content.put("ut_kqjl_cardtime", currentTime);
            content.put("ut_kqjl_cardip", request.getRemoteAddr());
            makeKqjl(content);
            response.getWriter().println("成功!（签退时间"+currentTime+"）");
        }

    }

    private String makeKqjl(Map content) {//插入考勤记录
        FormdataServiceImpl si = new FormdataServiceImpl();
        Formdata formdata = new Formdata();
        formdata.setOperator(Env.getRemoteUser().getId());
        formdata.setTypeid("4028818336aa84340136aabf54ef0017"); //考勤记录目录
        Dataset ds = new Dataset();
        List<Cell> mainlist = new ArrayList(); //主表单元格
        Cell c1 = new Cell(); //打卡人
        c1.setId("ut_kqjl_humanid");
        c1.setValue((String) content.get("ut_kqjl_humanid"));
        Cell c2 = new Cell();//打卡日期
        c2.setId("ut_kqjl_carddate");
        c2.setValue((String) content.get("ut_kqjl_carddate"));
        Cell c3 = new Cell();//打卡时间
        c3.setId("ut_kqjl_cardtime");
        c3.setValue((String) content.get("ut_kqjl_cardtime"));
        Cell c4 = new Cell();//ip
        c4.setId("ut_kqjl_cardip");
        c4.setValue((String) content.get("ut_kqjl_cardip"));
        mainlist.add(c1);
        mainlist.add(c2);
        mainlist.add(c3);
        mainlist.add(c4);
        ds.setMaintable(mainlist);
        formdata.setData(ds);
        String processid = si.createFormdata(formdata);
        return processid;
    }
}
